//
//  JoinUserViewController.m
//  MJApp
//
//  Created by 王亮 on 15/7/22.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "JoinUserViewController.h"

#import "JoinUserTableViewCell.h"

@interface JoinUserViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *joinUserArray;
@property(nonatomic,strong)NSString *ID;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation JoinUserViewController


- (instancetype)initWithArray:(NSArray *)array {
    
    if (self = [super init]) {
        
        self.joinUserArray = array;
    }
    return self;
}

- (instancetype)initWithID:(NSString *)ID {
    
    if (self = [super init]) {
        
        self.ID = ID;
        [self obationData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)obationData {
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"id"] = self.ID;
    [WLHTTPRequest getActivityDetailWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        self.joinUserArray =resultDictionary[@"data"][@"join_users"];
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)UI {
    
    self.title = @"已报名";
    self.view.backgroundColor = Color_Back_Gray;
    CGRect frame = self.view.frame;
    CGFloat side = 10;
    frame.origin.x = side;
    frame.origin.y = side;
    frame.size.width -= 2 * side;
    frame.size.height -= 2 * side + 64;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorColor = [UIColor clearColor];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.joinUserArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIView *cellView = WL_XIB(@"JoinUserTableViewCell");
    return cellView.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"JoinUserTableViewCell";
    JoinUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell = WL_XIB(@"JoinUserTableViewCell");
    }
    cell.infoDic = self.joinUserArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 35)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 25)];
    label.text = [NSString stringWithFormat:@"已报名 | %lu",(unsigned long)self.joinUserArray.count];
    [headView addSubview:label];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12, CGRectGetHeight(label.frame), CGRectGetWidth(tableView.frame) - 24, 10)];
    imageView.image = [UIImage imageNamed:@"11.png"];
    [headView addSubview:imageView];
    return headView;
}


@end
